// pages/diary/diary.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _navbar:wx.getStorageSync('_navbar') || [],
    __wxapp_img:wx.getStorageSync('WXAPP_IMG') || {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDiary(this)
  },
  getDiary(t){
    getApp().request({
      url:getApp().api.siteinfo.apiroot+'diary/index',
      data:{
        storeId:getApp().api.siteinfo.acid,
        userId:wx.getStorageSync('ACCESS_TOKEN')
      },
      success(d){
        t.setData({
          diary:d.diarys
        })
      }
    })
  },
  dianzan(e){
    var t = this, d = e.currentTarget.dataset;
    d.item.is_zan == 2 && getApp().request({
      url:getApp().api.siteinfo.apiroot+'diary/zan',
      data:{
        diaryId:d.item.id,
        userId:wx.getStorageSync('ACCESS_TOKEN')
      },
      success(){
        t.data.diary[d.idx].is_zan = 1;
        t.setData({
          diary:t.data.diary
        })
      }
    })
  },
  showShareModal: function(e) {
    var t = this;
    getApp().request({
      url:getApp().api.siteinfo.apiroot+'diary/queryDiaryShareInfo',
      data:{
        diaryId:e.currentTarget.dataset.item.id,
        userId:wx.getStorageSync('ACCESS_TOKEN')
      },
      success(m){
        t.setData({
          share_modal_active: "active",
          no_scroll: !0,
          show_share_modal_data:m,
          show_share_modal_item:e.currentTarget.dataset.item
        });
      }
    })
  },
  shareModalClose: function() {
    this.setData({
      share_modal_active: "",
      no_scroll: !1
    });
  },
  goodsQrcodeClose: function() {
    this.setData({
      goods_qrcode_active: "",
      no_scroll: !1,
      goods_qrcode:''
    });
  },
  getGoodsQrcode: function() {
    var realImg;
    var t = this;
    t.setData({
      goods_qrcode_active: "active", share_modal_active: ""
    });
    getApp().request({
      url: getApp().api.siteinfo.apiroot+'action/imgCode',
      data: {
        storeId:getApp().api.siteinfo.acid,
        diaryId:t.data.show_share_modal_item.id,
        userToken: wx.getStorageSync('ACCESS_TOKEN'),
        pageUrl:'pages/index/index'
      },
      success: function(o) {
        console.log("realImg=" + o.imgurl);
        0 == o.result && t.setData({
          goods_qrcode: o.imgurl
        }), -1 == o.result && (t.goodsQrcodeClose(), getApp().core.showModal({
          title: "提示",
          content: o.errmsg,
          showCancel: !1
        }));
      }
    });
  },
  saveGoodsQrcode: function() {
    var e = this;
    getApp().core.saveImageToPhotosAlbum ? (getApp().core.showLoading({
      title: "正在保存图片",
      mask: !1
    }), getApp().core.downloadFile({
      url: e.data.goods_qrcode,
      success: function(o) {
        getApp().core.showLoading({
          title: "正在保存图片",
          mask: !1
        }), getApp().core.saveImageToPhotosAlbum({
          filePath: o.tempFilePath,
          success: function() {
            getApp().core.showModal({
              title: "提示",
              content: "商品海报保存成功",
              showCancel: !1
            });
          },
          fail: function(o) {
            getApp().core.showModal({
              title: "图片保存失败",
              content: o.errMsg,
              showCancel: !1
            });
          },
          complete: function(o) {
            getApp().core.hideLoading();
          }
        });
      },
      fail: function(o) {
        getApp().core.showModal({
          title: "图片下载失败",
          content: o.errMsg + ";" + e.data.goods_qrcode,
          showCancel: !1
        });
      },
      complete: function(o) {
        getApp().core.hideLoading();
      }
    })) : getApp().core.showModal({
      title: "提示",
      content: "当前版本过低，无法使用该功能，请升级到最新版本后重试。",
      showCancel: !1
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if(this.data.show_share_modal_data.url){
      return {
        path:getApp().querystring(this.data.show_share_modal_item.url,'web'),
        title:this.data.show_share_modal_data.diaryName,
        imageUrl:this.data.show_share_modal_data.picurl
      }
    }
  },
  stopPropagation(){},
  goWebView(e){
    wx.navigateTo({
      url:getApp().querystring(e.currentTarget.dataset.url)
    })
  },
})